/*
 * Client.cpp
 *
 *  Created on: Mar 14, 2013
 *      Author: Xi
 */

#include "Client.h"
#include "TCPSocket.h"
#include<iostream>
#include "Utilities.h"
#include "ApplicationLayer.h"
#include<string.h>
#include<stdio.h>
#include<fstream>
#include<stdlib.h>

using namespace std;

bool receivedMsg = true;
string currentUser, userName;

Client::Client() {
	quit = false;
}

/**
 * Signal for main thread, after received response from server, get next input
 * @param b
 */
void Client::setReceivedMsg(bool b) {
	quit = false;
    receivedMsg = b;
}

/**
 * Keep track of current user
 * @param u
 */
void Client::setCurrentUser(string u) {
    cout << "Welcome " << userName << "!" << endl;
    currentUser = userName;
}

/**
 * Return signal
 * @return 
 */
bool Client::isReceivedMsg() {
    bool b = receivedMsg;
    return b;
}

/**
 * Default destructor
 */
Client::~Client() {
}

/**
 * Display result from server
 * @param res
 * @param fd
 */
void Client::displayResult(string res, int fd) {
    cout << res << endl;
    /*store user information*/

    if (res == "succeed")
        this->setCurrentUser(res);
}

void Client::parseInput(string a, int fd) {


    cout << "Please issue a command:" << endl;

    string cmd;
    cin >> cmd;
    //TODO convert all input to lowercase
    if (cmd == "register") {

        cout << "Please input a user name, length between 1 - 20:" << endl;
        string userName, userPass;
        cin >> userName;
        int len = this->utl.strLenCheck(userName);

        /*invalid length*/
        while (len < 1 || len > 20) {
            cout << "Please input a user name, length between 1 - 20:" << endl;
            cin >> userName;
        }

        cout << "Please input a user password, length between 8 - 20:" << endl;
        cin >> userPass;
        len = this->utl.strLenCheck(userPass);

        /*invalid length*/
        while (len < 8 || len > 20) {
            cout << "Please input a user password, length between 8 - 20:" << endl;
            cin >> userPass;
            len = this->utl.strLenCheck(userPass);
        }

        /*send out message*/
        this->app.fromCmd("register " + userName + " " + userPass, fd);


    } else if (cmd == "login") {

        if (currentUser.length() != 0) {
            cout << "Already logged in!" << endl;
            parseInput(cmd, fd);
        } else {

            cout << "Please input your user name, length between 1 - 20:" << endl;
            string userPass;
            cin >> userName;
            int len = this->utl.strLenCheck(userName);

            /*invalid length*/
            while (len < 1 || len > 20) {
                cout << "Please input your user name, length between 1 - 20:" << endl;
                userName;
                cin >> userName;
                len = this->utl.strLenCheck(userName);
            }

            cout << "Please input your password, length between 8 - 20:" << endl;
            cin >> userPass;
            len = this->utl.strLenCheck(userPass);

            /*invalid length*/
            while (len < 8 || len > 20) {
                cout << "Please input your password, length between 8 - 20:" << endl;
                cin >> userPass;
                len = this->utl.strLenCheck(userPass);
            }

            /*send out message*/
            this->app.fromCmd("login " + userName + " " + userPass, fd);
        }
    } else if (cmd == "list") {

        if (currentUser.length() == 0) {
            cout << "Please login first!" << endl;
            parseInput(cmd, fd);
        } else {
            cout << "Please input a date (yyyy-mm-dd), or '-' that no date specified: " << endl;
            string date;
            cin >> date;

            cout << "Please input a team, or '-' that no team specified:: " << endl;
            string team;
            cin >> team;

            if (team == "-" && date == "-") {
                //cout << "Please either specify team or date!" << endl;
                //parseInput("a", fd);
			    this->app.fromCmd("list - -",fd);
            } else if (team == "-") {
                /*send out message*/
                string temp = "- ";
                this->app.fromCmd("list " + temp + date, fd);
            } else if (date == "-") {
                /*send out message*/
                string temp = " -";
                this->app.fromCmd("list " + team + temp, fd);
            } else {
                /*send out message*/
                this->app.fromCmd("list " + team + " " + date, fd);
            }
        }
    } else if (cmd == "book") {

        if (currentUser.length() == 0) {
            cout << "Please login first!" << endl;
            parseInput(cmd, fd);
        } else {
            cout << "Please input game id:" << endl;
            string id, ticketType;
            cin >> id;

            cout << "Please input ticket type (VIP ,normal or economic):" << endl;
            cin >> ticketType;

            /* confirm */
//            cout << "Please confirm game id:" << endl;
 //           cin >> id;

            /*send out message*/
            this->app.fromCmd("book " + currentUser + " " + id + " " + ticketType, fd);
        }
    } else if (cmd == "pay") {
        if (currentUser.length() == 0) {
            cout << "Please login first!" << endl;
            parseInput(cmd, fd);
        } else {
	    string cNum;
	    int len = 0;
 	  while (len != 16) {
           	cout << "Please input your credit card number(16 digits):" << endl;
            cin >> cNum;
        	len = this->utl.strLenCheck(cNum);
			if (cNum.find_first_not_of("0123456789")!=string::npos){
				len = 0;
			}
       }
                        /*send out message*/
            this->app.fromCmd("pay " + cNum, fd);
        }
    } else if (cmd == "view") {
        if (currentUser.length() == 0) {
            cout << "Please login first!" << endl;
            parseInput(cmd, fd);
        } else {
            /*send out message*/

            this->app.fromCmd("view " + currentUser, fd);
        }

    } else if (cmd == "error") {
        if (currentUser.length() == 0) {
            cout << "Please login first!" << endl;
            parseInput(cmd, fd);
        } else {
            cout << "Please set error rate in range 0 - 100:" << endl;
            string errStr;
            cin >> errStr;

            int error = atoi(errStr.c_str());
            DataLinkLayer dl;
            dl.setErrorRate(error);

            /*next loop*/
            parseInput(cmd, fd);
        }
    } else if (cmd == "logoff") {
		cout<<"Bye bye "<<userName<<", have a nice day!\n";
		userName = "";
		currentUser = "";
		parseInput(cmd, fd);

    } else if (cmd == "echo_debug") {
        this->utl.displayDebugInfo();
        parseInput(cmd, fd);
    } else if (cmd == "hide_debug") {
        this->utl.hideDebugInfo();
        parseInput(cmd, fd);
    } 
	else if (cmd=="quit"){
		quit = true;
	}else {
        cout << "--------command help--------" << endl;
        cout << "register" << " --register a new account" << endl;
        cout << "login" << " --login by provide user name and password" << endl;
        cout << "list team date" << " --list all games by specify date and team" << endl;
        cout << "book game_id" << " --book a ticket by provide game_id" << endl;
//        cout << "confirm game_id" << "confirm a order by provide game_id again" << endl;
        cout << "pay credit_card" << " --pay for the ticket by provide credit card  number" << endl;
        cout << "view" << " -- view all ordering  history" << endl;
        cout << "echo_debug" << " -- echo all debug information" << endl;
        cout << "hide_debug" << " -- hide all debug information" << endl;
        cout << "error" << " -- set error from 0 - 100, 0 means 0% error and 100 means 100% error" << endl;
        cout << "logoff" << " -- logout" << endl;
		cout << "quit" << " -- quit client" <<endl;
        cout << "-------------------------------------" << endl;
        parseInput(cmd, fd);
    }
}



